#include "MyShots.h"
#include "MyShot.h"
#include "MyGame.h"
#include "MyDefinitions.h"
#include "MyUniversal.h"
#include "MyEngine.h"

MyShots::MyShots(MyGame* _parent){ parent = _parent; }
MyShots::~MyShots()
{ 
	clean_projectils();
	unload_surface(shotSheet);
	
#ifdef _DEBUG
	printf("destroyed projectils list\n");
#endif
}

//INIT -- load images to be used as projectils, player, scenery, (shots?)
void MyShots::load_imgs()
{    //Load the button sprite sheet    	
	#if defined(_WIN32)
	shotSheet = load_image("misc\\shotsheet.png");
	spellSheet = load_image("misc\\spellsheet.png");
	#else
	shotSheet = load_image("misc/shotsheet.png");
	spellSheet = load_image("misc/spellsheet.png");
	#endif
		
    //If there was a problem in loading the projectil sheets
	if ((shotSheet == NULL) || (spellSheet == NULL))
	{	
		printf("ERROR:Cannot load game images! (check 'shotsheet.png' and 'spellsheet.png')");
		parent->demand_quit();
	}	    
}
void MyShots::init_projectils()
{
	load_imgs();
	//fill the shotTypeArray with shots of different types, all the types must be added to enum 

	//ST_STD_BOW stats
	shotTypeArray[ST_STD_BOW].pos_in_sheet = ST_STD_BOW;
	shotTypeArray[ST_STD_BOW].shot_w = 11;
	shotTypeArray[ST_STD_BOW].shot_h = 11;
	shotTypeArray[ST_STD_BOW].speed_initial = INITIAL_SHOT_SPEED; 
	shotTypeArray[ST_STD_BOW].ballistic = true;
	shotTypeArray[ST_STD_BOW].phase_count = 36;
	shotTypeArray[ST_STD_BOW].mana_consume = 0;
	
	//ST_FIRE_BOW stats
	shotTypeArray[ST_FIRE_BOW].pos_in_sheet = ST_FIRE_BOW;
	shotTypeArray[ST_FIRE_BOW].shot_w = 11;
	shotTypeArray[ST_FIRE_BOW].shot_h = 11;
	shotTypeArray[ST_FIRE_BOW].speed_initial = INITIAL_SHOT_SPEED * 1.2; 
	shotTypeArray[ST_FIRE_BOW].ballistic = true;
	shotTypeArray[ST_FIRE_BOW].phase_count = 36;
	shotTypeArray[ST_FIRE_BOW].mana_consume = 0;
	
	//ST_ICE_BOW stats
	shotTypeArray[ST_ICE_BOW].pos_in_sheet = ST_ICE_BOW;
	shotTypeArray[ST_ICE_BOW].shot_w = 11;
	shotTypeArray[ST_ICE_BOW].shot_h = 11;
	shotTypeArray[ST_ICE_BOW].speed_initial = INITIAL_SHOT_SPEED * 1.5; 
	shotTypeArray[ST_ICE_BOW].ballistic = true;
	shotTypeArray[ST_ICE_BOW].phase_count = 36;
	shotTypeArray[ST_ICE_BOW].mana_consume = 0;

	//ST_HIT stats
	shotTypeArray[ST_HIT].pos_in_sheet = ST_HIT;
	shotTypeArray[ST_HIT].shot_w = 11;
	shotTypeArray[ST_HIT].shot_h = 11;
	shotTypeArray[ST_HIT].speed_initial = INITIAL_SHOT_SPEED; 
	shotTypeArray[ST_HIT].ballistic = false; 
	shotTypeArray[ST_HIT].phase_count = 2;
	shotTypeArray[ST_HIT].mana_consume = 0;

	//ST_FIREBALL stats
	shotTypeArray[ST_FIREBALL].pos_in_sheet = ST_FIREBALL;
	shotTypeArray[ST_FIREBALL].shot_w = 22;
	shotTypeArray[ST_FIREBALL].shot_h = 22;
	shotTypeArray[ST_FIREBALL].speed_initial = INITIAL_SHOT_SPEED * 0.6; 
	shotTypeArray[ST_FIREBALL].ballistic = false; 
	shotTypeArray[ST_FIREBALL].phase_count = 8;
	shotTypeArray[ST_FIREBALL].mana_consume = 2;

	//ST_FROSTBALL stats
	shotTypeArray[ST_FROSTBALL].pos_in_sheet = ST_FROSTBALL;
	shotTypeArray[ST_FROSTBALL].shot_w = 22;
	shotTypeArray[ST_FROSTBALL].shot_h = 22;
	shotTypeArray[ST_FROSTBALL].speed_initial = INITIAL_SHOT_SPEED*0.5; 
	shotTypeArray[ST_FROSTBALL].ballistic = false; 
	shotTypeArray[ST_FROSTBALL].phase_count = 16;
	shotTypeArray[ST_FROSTBALL].mana_consume = 3;

	//ST_LIGHTBALL stats
	shotTypeArray[ST_LIGHTBALL].pos_in_sheet = ST_LIGHTBALL;
	shotTypeArray[ST_LIGHTBALL].shot_w = 22;
	shotTypeArray[ST_LIGHTBALL].shot_h = 22;
	shotTypeArray[ST_LIGHTBALL].speed_initial = INITIAL_SHOT_SPEED * 2; 
	shotTypeArray[ST_LIGHTBALL].ballistic = false; 
	shotTypeArray[ST_LIGHTBALL].phase_count = 16;
	shotTypeArray[ST_LIGHTBALL].mana_consume = 10;
	
	//clear the list for pointer holding
	shotSwarm = std::list<MyShot>();
}
//DEINIT -- clears the whole projectile list
void MyShots::clean_projectils () {	shotSwarm.clear(); }

//DRAW -- displays all the projectiles on screen, moves them before
void MyShots::draw_projectils(int _t){	
	std::list<MyShot>::iterator i;
	for (i = shotSwarm.begin();i != shotSwarm.end();)
			//out of screen bounds/landed --> true returned, remove
			if (i->move_shot(_t)) shotSwarm.erase (i++);
			else ++i;
}

//ADD -- inserts new projectile object into the list
void MyShots::add_projectile(double _x, double _y, double _angle, shotType* _type, int owner, int _dmg)
{	
	MyShot newShot;
	newShot.init_shot(_x,_y,_angle, _type, this, owner,_dmg);
	shotSwarm.push_back(newShot);
}
